﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "QtNotificationMessageBox.h"

#include "INotification.h"
#include "UiNotificationManager.h"
#include "GbmpNew.h"
#include "StringUtil.h"
#include "UiCommonDialog.h"

#include <QBoxLayout>
#include <QApplication>
#include <QKeyEvent>
#include <QPushButton>



#include "IUiDocumentViewManager.h"
#include "IUiView.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

// QtNotificationMessageBox 类实现
namespace
{
    const int s_iconSize = 16;
    const int s_spacing = 6;
    const int s_textMaxSize = 15;
    const std::wstring s_detailButtonText = GBMP_TR(L"查看详情");
}

namespace gcmp
{
    void setTextForQLabel(QLabel* label, std::wstring str)
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(label, L"label为空！",L"GDMPLab",L"2024-03-30");
        label->setText(QString::fromStdWString(str));
        label->setAttribute(Qt::WA_TransparentForMouseEvents, true);
        label->setStyleSheet("font: 12px \"Microsoft YaHei UI\"; color: white;");
        if (str.size() > s_textMaxSize)
        {
            label->setFixedWidth(s_textMaxSize * 12);
            label->setWordWrap(true);
        }
    }

    void setIconForQLabel(QLabel* label, NotificationTypes type)
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(label, L"label为空！",L"GDMPLab",L"2024-03-30");
        label->setAttribute(Qt::WA_TransparentForMouseEvents,true);
        label->setFixedSize(s_iconSize, s_iconSize);
        std::wstring iconPath = L":/images/notification/ok.png";
        switch (type)
        {
        case NotificationTypes::InfomationNotificationType:
            iconPath = L":/images/notification/info.png";
            break;
        case NotificationTypes::WarningNotificationType:
            iconPath = L":/images/notification/warning.png";
            break;
        default:
            break;
        }

        QImage img(QString::fromStdWString(iconPath));
        if (!img.isNull())
        {
            QPixmap pix = QPixmap::fromImage(img);
            label->setPixmap(pix);
            label->setScaledContents(true);
        }
    }
}

gcmp::QtNotificationMessageBox::QtNotificationMessageBox(const INotification* notification, QWidget* pParent, bool transparent) :
    QWidget(pParent, Qt::FramelessWindowHint | Qt::Tool),
    m_notification(notification)
{
    setContentsMargins(s_spacing, s_spacing, s_spacing, s_spacing);
    setStyleSheet("background-color: rgba(0, 0, 0, 120);");
    if (transparent)
    {
        setMouseTracking(true);
        installEventFilter(this);
    }

    auto type = NEW_AS_OWNER_PTR(QLabel);
    m_notificationType = type.get();
    setIconForQLabel(type.get(), notification->GetType());

    auto textLayout = NEW_AS_OWNER_PTR(QVBoxLayout);
    textLayout->setSpacing(s_spacing);
    textLayout->setContentsMargins(0, 0, 0, 0);

    const std::wstring& str = notification->GetText();
    auto text = NEW_AS_OWNER_PTR(QLabel);
    m_notificationText = text.get();
    setTextForQLabel(text.get(), str);

    textLayout->addWidget(TRANSFER_OWNERSHIP_TO_THIRD_PARTY(text));

    if (notification->GetDetailItems().size() > 0)
    {
        auto detail = NEW_AS_OWNER_PTR(QPushButton, QString::fromStdWString(s_detailButtonText));
        detail->setStyleSheet("font: 12px \"Microsoft YaHei UI\"; color: #0635D7; border-width: 1px; border-style: none none solid none; border-color: #0635D7;");
        detail->setFixedWidth(detail->sizeHint().width());
        detail->setCursor(QCursor(Qt::PointingHandCursor));
        QObject::connect(detail.get(), SIGNAL(clicked()), this, SLOT(OnDetailClick()));
        textLayout->addWidget(TRANSFER_OWNERSHIP_TO_THIRD_PARTY(detail));
    }

    auto layout = NEW_AS_OWNER_PTR(QHBoxLayout);
    layout->setSpacing(s_spacing);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->addWidget(TRANSFER_OWNERSHIP_TO_THIRD_PARTY(type), 0, Qt::AlignTop);
    layout->addLayout(TRANSFER_OWNERSHIP_TO_THIRD_PARTY(textLayout));

    setLayout(TRANSFER_OWNERSHIP_TO_THIRD_PARTY(layout));

}

bool gcmp::QtNotificationMessageBox::eventFilter(QObject *object, QEvent *event)
{
    QKeyEvent* qKeyEvent = dynamic_cast<QKeyEvent*>(event);
    QMouseEvent* qMouseEvent = dynamic_cast<QMouseEvent*>(event);
    if (qKeyEvent || qMouseEvent)
    {
        IUiView* pUIView = IUiDocumentViewManager::Get()->GetCurrentUiView();
        if (!pUIView)
            return QWidget::eventFilter(object, event);

        QWidget* widget = QWidget::find((WId)pUIView->GetUiViewId());
        DBG_WARN_AND_RETURN_FALSE_UNLESS(widget, L"widget为空",L"GDMPLab",L"2024-03-30");
        QApplication::sendEvent(widget, event);
        return true;
    }
    return QWidget::eventFilter(object, event);
}

void gcmp::QtNotificationMessageBox::OnDetailClick()
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_notification, L"m_notification为空！",L"GDMPLab",L"2024-03-30");

    UiCommonDialog::ShowMessageBox(s_detailButtonText, m_notification->GetText(), (int)UiCommonDialog::ButtonType::OK);
}

void gcmp::QtNotificationMessageBox::SetNotification(const INotification* notification)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_notificationText, L"m_nofificationText为空！",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_notificationType, L"m_notificationType为空！",L"GDMPLab",L"2024-03-30");

    const std::wstring& str = notification->GetText();
    setTextForQLabel(m_notificationText, str);
    if(notification->GetType() != m_notification->GetType())
        setIconForQLabel(m_notificationType, notification->GetType());
    m_notification = notification;
}

